using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;

namespace PloxEdApp
{
    public partial class PloxStatus : Form
    {
        ServiceController ploxService;

        public PloxStatus()
        {
            InitializeComponent();
            ServiceController[] localServices = null; //To prevent an unassigned variable error.

            try
            {
                //Get a list of all services installed
                localServices = System.ServiceProcess.ServiceController.GetServices();
            }
            catch (Exception x)
            {
                MessageBox.Show("Unable to connect to the Service Control Manager. The error returned was: " + x.Message, "Oh No!");
                this.Dispose(); //Seppuku
            }

            int ploxServiceIndex = int.MinValue;

            for(int i = 0; i < localServices.Length; i++)
            {
                if (localServices[i].ServiceName.Equals("Plox"))
                {
                    ploxServiceIndex = i;
                    break;
                }
            }

            if (!ploxServiceIndex.Equals(int.MinValue)) //Plox is installed
            {
                this.instLabel.Text = "Installed";
                this.instPicture.Image = global::PloxEd.Properties.Resources.GreenSphere;
                this.ploxService = localServices[ploxServiceIndex];

                this.serviceLabel.Text = ploxService.Status.ToString();

                refreshUI();
            }
        } //End of PloxStatus method 

        private void refreshUI()
        {
            this.serviceLabel.Text = ploxService.Status.ToString();

            if (ploxService.Status.Equals(ServiceControllerStatus.Running))
            {
                this.servicePicture.Image = global::PloxEd.Properties.Resources.GreenSphere;

                this.stopButton.Enabled = true;
                this.startButton.Enabled = false;

                this.pauseContButton.Enabled = true;
                this.pauseContButton.Text = "Pause";
            }
            else if (ploxService.Status.Equals(ServiceControllerStatus.Paused))
            {
                this.servicePicture.Image = global::PloxEd.Properties.Resources.YellowSphere;

                this.startButton.Enabled = false;
                this.stopButton.Enabled = true;
                
                this.pauseContButton.Enabled = true;
                this.pauseContButton.Text = "Continue";
            }
            else if (ploxService.Status.Equals(ServiceControllerStatus.Stopped))
            {
                this.servicePicture.Image = global::PloxEd.Properties.Resources.RedSphere;

                this.startButton.Enabled = true;

                this.stopButton.Enabled = false;
                this.pauseContButton.Enabled = false;
            }
            else if (ploxService.Status.Equals(ServiceControllerStatus.StartPending))
            {
                this.servicePicture.Image = global::PloxEd.Properties.Resources.YellowSphere;

                this.startButton.Enabled = false;

                this.stopButton.Enabled = true;
                this.pauseContButton.Enabled = false;
            }
            else //Should seldom be in any other state
            {
                this.servicePicture.Image = global::PloxEd.Properties.Resources.YellowSphere;

                this.startButton.Enabled = false;
                this.stopButton.Enabled = false;
                this.pauseContButton.Enabled = false;
            }
        } //End of refreshUI

        private void startButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            try
            {
                this.ploxService.Start();
                this.ploxService.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 15));
            }
            catch (System.ServiceProcess.TimeoutException timeX)
            {
                MessageBox.Show("Unable to start Plox in a reasonable amount of time. This usually" + '\n'
                    + "means that there is nothing for Plox to do. Please define a task" + '\n'
                    + "and save changes before attempting to start Plox again.", "Oh No!");

                timeX.ToString(); //Does nothing
            }
            catch (Exception x)
            {
                MessageBox.Show("Could not start Plox, the error returned was:" + '\n' + '\n' +
                    x.Message.ToString(), "Oh No!");
            }

            Cursor = Cursors.Default;
            refreshUI();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            try
            {
                this.ploxService.Stop();
                this.ploxService.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 15));
            }
            catch (Exception x)
            {
                MessageBox.Show("Could not stop Plox, the error returned was:" + '\n' + '\n' + 
                    x.Message.ToString(), "Oh No!");
            }

            Cursor = Cursors.Default;
            refreshUI();
        }

        private void pauseContButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            try
            {
                if (this.ploxService.Status.Equals(ServiceControllerStatus.Running))
                {
                    this.ploxService.Pause();
                    this.ploxService.WaitForStatus(ServiceControllerStatus.Paused, new TimeSpan(0, 0, 15));
                }
                else if (this.ploxService.Status.Equals(ServiceControllerStatus.Paused))
                {
                    this.ploxService.Continue();
                    this.ploxService.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 15));
                }
            }
            catch (Exception x)
            {
                MessageBox.Show("Could not pause or resume Plox, the error returned was:" + '\n' + '\n' +
                    x.Message.ToString(), "Oh No!");
            }

            Cursor = Cursors.Default;
            refreshUI();
        }
    } //End of PloxStatus class
}